package com.facebook.imagepipeline.animated.factory;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Build.VERSION;
import com.facebook.common.internal.Preconditions;
import com.facebook.common.references.CloseableReference;
import com.facebook.imagepipeline.animated.base.AnimatedDrawableBackend;
import com.facebook.imagepipeline.animated.base.AnimatedImage;
import com.facebook.imagepipeline.animated.base.AnimatedImageResult;
import com.facebook.imagepipeline.animated.base.AnimatedImageResultBuilder;
import com.facebook.imagepipeline.animated.impl.AnimatedDrawableBackendProvider;
import com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor;
import com.facebook.imagepipeline.animated.impl.AnimatedImageCompositor.Callback;
import com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory;
import com.facebook.imagepipeline.common.ImageDecodeOptions;
import com.facebook.imagepipeline.image.CloseableAnimatedImage;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.EncodedImage;
import com.facebook.imagepipeline.memory.PooledByteBuffer;
import java.util.ArrayList;
import java.util.List;

public class AnimatedImageFactoryImpl
  implements AnimatedImageFactory
{
  static AnimatedImageDecoder sGifAnimatedImageDecoder = loadIfPresent("com.facebook.animated.gif.GifImage");
  static AnimatedImageDecoder sWebpAnimatedImageDecoder = loadIfPresent("com.facebook.animated.webp.WebPImage");
  private final AnimatedDrawableBackendProvider mAnimatedDrawableBackendProvider;
  private final PlatformBitmapFactory mBitmapFactory;

  public AnimatedImageFactoryImpl(AnimatedDrawableBackendProvider paramAnimatedDrawableBackendProvider, PlatformBitmapFactory paramPlatformBitmapFactory)
  {
    this.mAnimatedDrawableBackendProvider = paramAnimatedDrawableBackendProvider;
    this.mBitmapFactory = paramPlatformBitmapFactory;
  }

  @SuppressLint({"NewApi"})
  private CloseableReference<Bitmap> createBitmap(int paramInt1, int paramInt2, Bitmap.Config paramConfig)
  {
    CloseableReference localCloseableReference = this.mBitmapFactory.createBitmap(paramInt1, paramInt2, paramConfig);
    ((Bitmap)localCloseableReference.get()).eraseColor(0);
    if (Build.VERSION.SDK_INT >= 12)
      ((Bitmap)localCloseableReference.get()).setHasAlpha(true);
    return localCloseableReference;
  }

  private CloseableReference<Bitmap> createPreviewBitmap(AnimatedImage paramAnimatedImage, Bitmap.Config paramConfig, int paramInt)
  {
    CloseableReference localCloseableReference = createBitmap(paramAnimatedImage.getWidth(), paramAnimatedImage.getHeight(), paramConfig);
    AnimatedImageResult localAnimatedImageResult = AnimatedImageResult.forAnimatedImage(paramAnimatedImage);
    new AnimatedImageCompositor(this.mAnimatedDrawableBackendProvider.get(localAnimatedImageResult, null), new AnimatedImageCompositor.Callback()
    {
      public CloseableReference<Bitmap> getCachedBitmap(int paramAnonymousInt)
      {
        return null;
      }

      public void onIntermediateResult(int paramAnonymousInt, Bitmap paramAnonymousBitmap)
      {
      }
    }).renderFrame(paramInt, (Bitmap)localCloseableReference.get());
    return localCloseableReference;
  }

  private List<CloseableReference<Bitmap>> decodeAllFrames(AnimatedImage paramAnimatedImage, Bitmap.Config paramConfig)
  {
    final ArrayList localArrayList = new ArrayList();
    AnimatedImageResult localAnimatedImageResult = AnimatedImageResult.forAnimatedImage(paramAnimatedImage);
    AnimatedDrawableBackend localAnimatedDrawableBackend = this.mAnimatedDrawableBackendProvider.get(localAnimatedImageResult, null);
    AnimatedImageCompositor localAnimatedImageCompositor = new AnimatedImageCompositor(localAnimatedDrawableBackend, new AnimatedImageCompositor.Callback()
    {
      public CloseableReference<Bitmap> getCachedBitmap(int paramAnonymousInt)
      {
        return CloseableReference.cloneOrNull((CloseableReference)localArrayList.get(paramAnonymousInt));
      }

      public void onIntermediateResult(int paramAnonymousInt, Bitmap paramAnonymousBitmap)
      {
      }
    });
    for (int i = 0; i < localAnimatedDrawableBackend.getFrameCount(); i++)
    {
      CloseableReference localCloseableReference = createBitmap(localAnimatedDrawableBackend.getWidth(), localAnimatedDrawableBackend.getHeight(), paramConfig);
      localAnimatedImageCompositor.renderFrame(i, (Bitmap)localCloseableReference.get());
      localArrayList.add(localCloseableReference);
    }
    return localArrayList;
  }

  private CloseableAnimatedImage getCloseableImage(ImageDecodeOptions paramImageDecodeOptions, AnimatedImage paramAnimatedImage, Bitmap.Config paramConfig)
  {
    List localList = null;
    CloseableReference localCloseableReference = null;
    try
    {
      boolean bool1 = paramImageDecodeOptions.useLastFrameForPreview;
      localList = null;
      localCloseableReference = null;
      if (bool1);
      for (int i = -1 + paramAnimatedImage.getFrameCount(); ; i = 0)
      {
        boolean bool2 = paramImageDecodeOptions.decodeAllFrames;
        localList = null;
        localCloseableReference = null;
        if (bool2)
        {
          localList = decodeAllFrames(paramAnimatedImage, paramConfig);
          localCloseableReference = CloseableReference.cloneOrNull((CloseableReference)localList.get(i));
        }
        if ((paramImageDecodeOptions.decodePreviewFrame) && (localCloseableReference == null))
          localCloseableReference = createPreviewBitmap(paramAnimatedImage, paramConfig, i);
        CloseableAnimatedImage localCloseableAnimatedImage = new CloseableAnimatedImage(AnimatedImageResult.newBuilder(paramAnimatedImage).setPreviewBitmap(localCloseableReference).setFrameForPreview(i).setDecodedFrames(localList).build());
        return localCloseableAnimatedImage;
      }
    }
    finally
    {
      CloseableReference.closeSafely(localCloseableReference);
      CloseableReference.closeSafely(localList);
    }
  }

  private static AnimatedImageDecoder loadIfPresent(String paramString)
  {
    try
    {
      AnimatedImageDecoder localAnimatedImageDecoder = (AnimatedImageDecoder)Class.forName(paramString).newInstance();
      return localAnimatedImageDecoder;
    }
    catch (Throwable localThrowable)
    {
    }
    return null;
  }

  public CloseableImage decodeGif(EncodedImage paramEncodedImage, ImageDecodeOptions paramImageDecodeOptions, Bitmap.Config paramConfig)
  {
    if (sGifAnimatedImageDecoder == null)
      throw new UnsupportedOperationException("To encode animated gif please add the dependency to the animated-gif module");
    CloseableReference localCloseableReference = paramEncodedImage.getByteBufferRef();
    Preconditions.checkNotNull(localCloseableReference);
    try
    {
      if (!paramImageDecodeOptions.forceOldAnimationCode);
      for (boolean bool = true; ; bool = false)
      {
        Preconditions.checkState(bool);
        PooledByteBuffer localPooledByteBuffer = (PooledByteBuffer)localCloseableReference.get();
        CloseableAnimatedImage localCloseableAnimatedImage = getCloseableImage(paramImageDecodeOptions, sGifAnimatedImageDecoder.decode(localPooledByteBuffer.getNativePtr(), localPooledByteBuffer.size()), paramConfig);
        return localCloseableAnimatedImage;
      }
    }
    finally
    {
      CloseableReference.closeSafely(localCloseableReference);
    }
  }

  public CloseableImage decodeWebP(EncodedImage paramEncodedImage, ImageDecodeOptions paramImageDecodeOptions, Bitmap.Config paramConfig)
  {
    if (sWebpAnimatedImageDecoder == null)
      throw new UnsupportedOperationException("To encode animated webp please add the dependency to the animated-webp module");
    CloseableReference localCloseableReference = paramEncodedImage.getByteBufferRef();
    Preconditions.checkNotNull(localCloseableReference);
    try
    {
      if (!paramImageDecodeOptions.forceOldAnimationCode);
      for (boolean bool = true; ; bool = false)
      {
        Preconditions.checkArgument(bool);
        PooledByteBuffer localPooledByteBuffer = (PooledByteBuffer)localCloseableReference.get();
        CloseableAnimatedImage localCloseableAnimatedImage = getCloseableImage(paramImageDecodeOptions, sWebpAnimatedImageDecoder.decode(localPooledByteBuffer.getNativePtr(), localPooledByteBuffer.size()), paramConfig);
        return localCloseableAnimatedImage;
      }
    }
    finally
    {
      CloseableReference.closeSafely(localCloseableReference);
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.facebook.imagepipeline.animated.factory.AnimatedImageFactoryImpl
 * JD-Core Version:    0.6.2
 */